package io.itit.ecp.admin.provide.action.controller.miniprogram;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.itit.ecp.admin.provide.action.req.CarbonSinkFactorQueryReq;
import io.itit.ecp.admin.provide.action.resp.CarbonSinkFactorResp;
import io.itit.ecp.admin.server.entity.CarbonSinkFactorEntity;
import io.itit.ecp.admin.server.service.ICarbonSinkFactorService;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：碳汇因子控制类
 * @Date：Create by 2023-11-23 16:12:43
 */
@Api(tags = "碳汇因子")
@RestController
@RequestMapping("/mini/carbonSinkFactor")
public class MiniCarbonSinkFactorController {

    @Resource
    private ICarbonSinkFactorService carbonSinkFactorService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    public ResultResp<IPage<CarbonSinkFactorResp>> queryPage(@RequestBody @Validated(IQueryPage.class) CarbonSinkFactorQueryReq req) {
        Page<CarbonSinkFactorEntity> page = carbonSinkFactorService.page(PageUtils.getPage(req, CarbonSinkFactorEntity.class), buildQW(req));
        return ResultResp.success(ConvertUtils.convert(page, CarbonSinkFactorResp.class));
    }

    /**
     * 组装查询条件
     */
    private static LambdaQueryWrapper<CarbonSinkFactorEntity> buildQW(CarbonSinkFactorQueryReq req) {
        LocalDateTime dateStart = null;
        LocalDateTime dateEnd = null;
        if (StringUtils.isNotEmpty(req.getSolidifyDate())) {
            YearMonth yearMonth = YearMonth.parse(req.getSolidifyDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            dateStart = yearMonth.atDay(1).atTime(LocalTime.MIN);
            dateEnd = LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), yearMonth.lengthOfMonth()).atTime(LocalTime.MAX);
        }
        return Wrappers.<CarbonSinkFactorEntity>lambdaQuery()
                .ge(StringUtils.isNotEmpty(req.getSolidifyDateStart()), CarbonSinkFactorEntity::getSolidifyDate, req.getSolidifyDateStart())
                .le(StringUtils.isNotEmpty(req.getSolidifyDateEnd()), CarbonSinkFactorEntity::getSolidifyDate, req.getSolidifyDateEnd())
                .eq(StringUtils.isNotEmpty(req.getType()), CarbonSinkFactorEntity::getType, req.getType())
                .eq(req.getFactor() != null, CarbonSinkFactorEntity::getFactor, req.getFactor())
                .eq(StringUtils.isNotEmpty(req.getUnit()), CarbonSinkFactorEntity::getUnit, req.getUnit())
                .eq(StringUtils.isNotEmpty(req.getRemark()), CarbonSinkFactorEntity::getRemark, req.getRemark())
                .eq(StringUtils.isNotEmpty(req.getDeptId()), CarbonSinkFactorEntity::getDeptId, req.getDeptId())
                .in(CollectionUtils.isNotEmpty(req.getIds()), CarbonSinkFactorEntity::getId, req.getIds())
                .ge(Objects.nonNull(dateStart), CarbonSinkFactorEntity::getSolidifyDate, dateStart)
                .lt(Objects.nonNull(dateEnd), CarbonSinkFactorEntity::getSolidifyDate, dateEnd)
                .last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
                .orderByDesc(StringUtils.isEmpty(req.getSortColumn()), CarbonSinkFactorEntity::getSolidifyDate);
    }
}
